package com.glory.wxmemsicpda.activity

import android.view.View
import androidx.databinding.DataBindingUtil
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.base.BaseDialogActivity
import com.glory.wxmemsicpda.bean.MaterialsListBean
import com.glory.wxmemsicpda.bean.net.BaseResponseBean
import com.glory.wxmemsicpda.databinding.ActivityChangeMaterialsBinding
import com.glory.wxmemsicpda.utils.NetUtils
import com.google.gson.JsonObject
import com.lzy.okgo.model.Response
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_SimpleDialogEntityCallBack
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils
import kotlinx.android.synthetic.main.activity_change_materials.*

class ChangeMaterialsActivity : BaseDialogActivity() {

    private var viewDataBinding : ActivityChangeMaterialsBinding?= null


    override fun getLayoutResId(): Int {
        return 0
    }

    override fun initView() {
        viewDataBinding = DataBindingUtil.setContentView(this,R.layout.activity_change_materials)
        var entity : MaterialsListBean.MaterialsBean = intent.getSerializableExtra("MATERIAL_BEAN") as MaterialsListBean.MaterialsBean
        viewDataBinding?.productBean = entity
        viewDataBinding?.eqpID = intent.getStringExtra("EQP_ID")
        val isAppend = intent.getStringExtra("APPEND")
        setTitle("材料更换")

        etLotId.requestFocus()
        FreeApi_EditTextUtils.setEditTextListener(etLotId,this, FreeUI_TextCallback {
            entity.materiaL_NO = it
            viewDataBinding?.productBean = entity
        })
        freeUI_titleView.setRightListener("更换", View.OnClickListener {
            if (etLotId.text.toString() == ""){
                showToast("请扫描物料二维码！", false)
                return@OnClickListener
            }
            //更换
            val map = JsonObject()
            map.addProperty("EQP_ID", viewDataBinding?.eqpID)
            map.addProperty("MATERIAL_TYPE", entity.materiaL_TYPE)
            map.addProperty("MATERIAL_NO", entity.materiaL_NO)
            map.addProperty("APPEND", isAppend)
            NetUtils.requestPosNet(this,
                "PDAEQPMatChange","PDAEQPMatChange",
                map,
                object : FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<MaterialsListBean?>?>(this) {
                    override fun onSuccess(response: Response<BaseResponseBean<MaterialsListBean?>?>) {
                        super.onSuccess(response)
                        if (response.body()!!.isSuccess(true, supportFragmentManager)) {
                            showToast("更换成功",true)
                            finish()
                        }
                    }

                    override fun onError(response: Response<BaseResponseBean<MaterialsListBean?>?>) {
                        super.onError(response)
                        loadError(response.exception, "PDAEQPMatChange")
                    }
                })
        })
    }
    override fun initData() {}
    override fun getBaseEntity(): FreeUI_AddViewEntity {
        return FreeUI_AddViewEntity("header", llMain)
    }
}